<?php

namespace paper\helper;

trait DotAccessTrait
{
    protected function dotAccess(string $key, array $array, $default = null)
    {
        if (str_contains($key, '.')) {
            $vars = explode('.', $key);
            $tmp = &$array;
            foreach ($vars as $field) {
                if (!isset($tmp[$field])) {
                    $tmp = $default;
                    break;
                }
                $tmp = $tmp[$field];
            }
            return $tmp;
        }
        return $array[$key] ?? $default;
    }
}